﻿namespace Higer.SharePoint.HigerWebService
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.Xml.Linq;
    using System.Data.SqlClient;
    using Higer.SharePoint.HigerWebService.HigerCls;

   /// <summary>
   /// HigerWorkList 的摘要说明
   /// </summary>
   [WebService(Namespace = "http://tempuri.org/")]
   [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
   [ToolboxItem(false)]
   // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
   // [System.Web.Script.Services.ScriptService]
   public class HigerWorkList : System.Web.Services.WebService
   {
      #region 公告类型枚举

      public enum emBulltinsType
      {
         全部,
         有效
      }

      #endregion

      #region 海格-员工任务获取

      [WebMethod(Description = "<h3>获取海格员工的当前任务</h3><p>输入参数：strUserId = 海格员工ID，返回数据：DataSet。</p>")]
      public DataSet GetMyTasks(string strUserId)
      {
         // 定义执行语句
         string strSql = "prj_klsz_qry_mytask_list_sp '" + strUserId + "','" + HigerDbBlock.GetAppSetting("MyTaskUrl") + "'";

         // 数据库连接获取字串
         string strCnString = "HigerPdm";

         return GetComDataSet(strSql, strCnString);
      }
      #endregion

      #region 海格-公告获取

      [WebMethod(Description = "<h3>获取海格员工的公告信息</h3><p>输入参数：strUserId = 海格员工ID，emBulltinsType = 获取公告的范围，返回数据：DataSet。</p>")]
      public DataSet GetBulltins(string strUserId, emBulltinsType emBulltinsType)
      {
         // 定义公告查询范围
         string strIsExpire = "N";

         if (emBulltinsType == emBulltinsType.全部)
         {
            strIsExpire = "Y";
         }

         // 定义执行语句
         string strSql = "sam_klsz_qry_bulletin_list_sp '" + strUserId + "','" + strIsExpire + "'";

         // 数据库连接获取字串
         string strCnString = "Hrms";

         return GetComDataSet(strSql, strCnString);
      }

      #endregion

      #region 海格-公告访问回写

      [WebMethod(Description = "<h3>对海格员工访问的公告进行登记</h3><p>输入参数：strUserId = 海格员工ID，strContentId = 公告ID，返回数据：Bool。</p>")]
      public bool WriteReadContentRecord(string strUserId, string strContentId)
      {
         // 定义数据库连接
         using (SqlConnection objSqlCn = new SqlConnection(HigerDbBlock.GetDbCnString("Hrms")))
         {
            // 定义SQL语句
            string strSql = "moss_klsz_insert_read_content_record_sp";

            // 定于数据库COM
            using (SqlCommand objSqlCom = new SqlCommand(strSql,objSqlCn))
            {
               try
               {
                  // 打开数据库连接
                  objSqlCn.Open();

                  // 设置数据库COM
                  objSqlCom.CommandType = CommandType.StoredProcedure;
                  objSqlCom.Parameters.AddWithValue("@@strUserId", strUserId);
                  objSqlCom.Parameters.AddWithValue("@@strContentId", strContentId);
                  objSqlCom.Parameters.Add("@@bolRvl", SqlDbType.Bit).Direction = ParameterDirection.Output;

                  objSqlCom.ExecuteNonQuery();

                  return (bool) objSqlCom.Parameters["@@bolRvl"].Value;
               }
               catch (DataException de)
               {
                  // 抛出异常
                  throw de;
               }
               finally
               {
                  // 关闭数据库连接
                  if (objSqlCn.State == ConnectionState.Open)
                  {
                     objSqlCn.Close();
                  }
               }
            }
         }
      }

      #endregion

      #region 海格ERP登录信息写入

      /// <summary>
      /// 写入ERP登录信息
      /// 1，删除当前IP的登录信息
      /// 2，写入新的信息
      /// </summary>
      /// <param name="clientIP">客户端IP</param>
      /// <param name="sessionID">SessionID</param>
      /// <param name="logonTime">登录时间</param>
      /// <param name="sessionStartTime">Session开始时间（实际上和登录时间相同）</param>
      /// <returns>写入成功返回OK，失败返回错误信息</returns>
      [WebMethod(Description = "<h3>写入ERP登录信息</h3><p>输入参数：strClientIP = 客户端IP，strSessionID = 系统ID，strLogonTime = 登录时间，strSessionStartTime = Session开始时间，strUserId = 登录用户ID，返回数据：string。</p>")]
      public string WriteERPLogonInfor(string strClientIP, string strSessionID, string strLogonTime, string strSessionStartTime,string strUserId)
      {
         // 定义数据库连接
         using (SqlConnection objSqlCn = new SqlConnection(HigerDbBlock.GetDbCnString("Hrms")))
         {
            // 定义SQL语句
            string strSql = "moss_klsz_insert_oa_logon_record_sp";

            // 定于数据库COM
            using (SqlCommand objSqlCom = new SqlCommand(strSql, objSqlCn))
            {
               try
               {
                  // 打开数据库连接
                  objSqlCn.Open();

                  // 设置数据库COM
                  objSqlCom.CommandType = CommandType.StoredProcedure;
                  objSqlCom.Parameters.AddWithValue("@@strUserId", strUserId);
                  objSqlCom.Parameters.AddWithValue("@@strClientIP", strClientIP);
                  objSqlCom.Parameters.AddWithValue("@@strSessionID", strSessionID);
                  objSqlCom.Parameters.AddWithValue("@@strLogonTime", strLogonTime);
                  objSqlCom.Parameters.AddWithValue("@@strSessionStartTime", strSessionStartTime);

                  objSqlCom.ExecuteNonQuery();

                  return "OK";
               }
               catch (DataException de)
               {
                  // 抛出异常
                  throw de;
               }
               finally
               {
                  // 关闭数据库连接
                  if (objSqlCn.State == ConnectionState.Open)
                  {
                     objSqlCn.Close();
                  }
               }
            }
         }
      }

      #endregion

      #region 海格-特殊订单技术任务获取

      [WebMethod(Description = "<h3>获取海格员工的特殊订单技术任务</h3><p>输入参数：strUserId = 海格员工ID，返回数据：DataSet。</p>")]
      public DataSet GetOrderTasks(string strUserId)
      {
         // 定义执行语句
         string strSql = "ord2_klsz_qry_WaitHandle_tech_task_sp '" + strUserId + "',178,0";

         // 数据库连接获取字串
         string strCnString = "Klpdm";

         return GetComDataSet(strSql, strCnString);
      }

      #endregion

      #region 海格-待办事项获取

      [WebMethod(Description = "<h3>获取海格员工的待办事项</h3><p>输入参数：strUserId = 海格员工ID，返回数据：DataSet。</p>")]
      public DataSet GetWaitHandles(string strUserId)
      {
         // 定义执行语句
         string strSql = "sam_klsz_qry_wait_handle_list_sp '" + strUserId + "'";

         // 数据库连接获取字串
         string strCnString = "Capmsplan";

         return GetComDataSet(strSql, strCnString);
      }

      #endregion

      #region 海格-手机短消息发送

      [WebMethod(Description = "<h3>手机短消息插入</h3><p>输入参数：strUserList = 海格员工ID列表，strSmsContent = 手机短消息内容，返回数据：string。</p>")]
      public string SendSMS(string strUserList, string strSmsContent)
      {
         // 定义数据库连接
         using (SqlConnection objSqlCn = new SqlConnection(HigerDbBlock.GetDbCnString("Capmsplan")))
         {
            // 定义SQL语句
            string strSql = "moss_klsz_insert_send_sms_content_sp";

            // 定于数据库COM
            using (SqlCommand objSqlCom = new SqlCommand(strSql, objSqlCn))
            {
               try
               {
                  // 打开数据库连接
                  objSqlCn.Open();

                  // string[] aryUserId = strUserList.Split(";");
                  string[] aryUserId;
                  char[] chrSeparators = new char[] {','};
                  aryUserId = strUserList.Split(chrSeparators, StringSplitOptions.RemoveEmptyEntries);

                  for (int inti = 0; inti < aryUserId.Length; inti++)
                  {
                     // 设置数据库COM
                     objSqlCom.CommandType = CommandType.StoredProcedure;
                     objSqlCom.Parameters.AddWithValue("@@strUserId", aryUserId[inti]);
                     objSqlCom.Parameters.AddWithValue("@@strSmsContent", strSmsContent);

                     objSqlCom.ExecuteNonQuery();
                  }

                  return "OK";
               }
               catch (DataException de)
               {
                  // 抛出异常
                  throw de;
               }
               finally
               {
                  // 关闭数据库连接
                  if (objSqlCn.State == ConnectionState.Open)
                  {
                     objSqlCn.Close();
                  }
               }
            }
         }
      }

      #endregion

      #region 海格-数据集获取公共函数
      /// <summary>
      /// 获取数据集的公共函数
      /// </summary>
      /// <param name="strSql">SQL语句</param>
      /// <param name="strCnString">数据库字串</param>
      /// <returns></returns>
      public DataSet GetComDataSet(string strSql, string strCnString)
      {
         // 定义数据库连接 其中连接字符串用HigerDbBlock.GetDbCnString来获取
         using (SqlConnection objSqlCn = new SqlConnection(HigerDbBlock.GetDbCnString(strCnString)))
         {
            // 定义数据库连接池
            using (SqlDataAdapter objDa = new SqlDataAdapter(strSql, objSqlCn))
            {
               // 定义数据库数据集
               using (DataSet objDs = new DataSet())
               {
                  try
                  {
                     // 打开数据库连接
                     objSqlCn.Open();

                     // 填充数据集
                     objDa.Fill(objDs);

                     // 返回数据集
                     return objDs;
                  }
                  catch (DataException de)
                  {
                     // 异常抛出
                     throw de;
                  }
                  finally
                  {
                     // 关闭数据库连接
                     if (objSqlCn.State == ConnectionState.Open)
                     {
                        objSqlCn.Close();
                     }
                  }
               }
            }
         }
      }

      #endregion
   }
}
